package net.zoneland.knowledge.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.jdbc.SQL;

import net.zoneland.knowledge.model.QaRecordsV2;
import net.zoneland.knowledge.model.QaRecordsV2Example;
import net.zoneland.knowledge.model.QaRecordsV2Example.Criteria;
import net.zoneland.knowledge.model.QaRecordsV2Example.Criterion;

public class QaRecordsV2SqlProvider {

    public String countByExample(QaRecordsV2Example example) {
        SQL sql = new SQL();
        sql.SELECT("count(*)").FROM("AITK_QA_RECORDS_V2");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String insertSelective(QaRecordsV2 record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("AITK_QA_RECORDS_V2");

        if (record.getId() != null) {
            sql.VALUES("ID", "#{id,jdbcType=VARCHAR}");
        }

        if (record.getCategory() != null) {
            sql.VALUES("CATEGORY", "#{category,jdbcType=VARCHAR}");
        }

        if (record.getQaType() != null) {
            sql.VALUES("QA_TYPE", "#{qaType,jdbcType=VARCHAR}");
        }

        if (record.getLevel1Title() != null) {
            sql.VALUES("LEVEL1_TITLE", "#{level1Title,jdbcType=VARCHAR}");
        }

        if (record.getLevel2Title() != null) {
            sql.VALUES("LEVEL2_TITLE", "#{level2Title,jdbcType=VARCHAR}");
        }

        if (record.getLevel3Title() != null) {
            sql.VALUES("LEVEL3_TITLE", "#{level3Title,jdbcType=VARCHAR}");
        }

        if (record.getQueryText() != null) {
            sql.VALUES("QUERY_TEXT", "#{queryText,jdbcType=VARCHAR}");
        }

        if (record.getQueStyle() != null) {
            sql.VALUES("QUESTYLE", "#{queStyle,jdbcType=VARCHAR}");
        }

        if (record.getReqId() != null) {
            sql.VALUES("REQID", "#{reqId,jdbcType=VARCHAR}");
        }

        if (record.getReqName() != null) {
            sql.VALUES("REQNAME", "#{reqName,jdbcType=VARCHAR}");
        }

        if (record.getCmpDn() != null) {
            sql.VALUES("CMPDN", "#{cmpDn,jdbcType=VARCHAR}");
        }

        if (record.getDeptDn() != null) {
            sql.VALUES("DEPTDN", "#{deptDn,jdbcType=VARCHAR}");
        }

        if (record.getCreateTime() != null) {
            sql.VALUES("CREATETIME", "#{createTime,jdbcType=TIMESTAMP}");
        }

        sql.VALUES("PRAISE", "#{praise,jdbcType=DECIMAL}");

        sql.VALUES("FINALANSWER", "#{finalAnswer,jdbcType=DECIMAL}");

        sql.VALUES("ANSCODE", "#{ansCode,jdbcType=DECIMAL}");

        if (record.getBankId() != null) {
            sql.VALUES("BANKID", "#{bankId,jdbcType=VARCHAR}");
        }

        if (record.getSessionId() != null) {
            sql.VALUES("SESSIONID", "#{sessionId,jdbcType=VARCHAR}");
        }

        if (record.getAnsContent() != null) {
            sql.VALUES("ANSCONTENT", "#{ansContent,jdbcType=CLOB}");
        }
        if (record.getVisitType() != null) {
            sql.VALUES("VISIT_TYPE", "#{visitType,jdbcType=VARCHAR}");
        }

        return sql.toString();
    }

    public String selectByExampleWithBLOBs(QaRecordsV2Example example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("ID");
        } else {
            sql.SELECT("ID");
        }
        sql.SELECT("CATEGORY");
        sql.SELECT("QA_TYPE");
        sql.SELECT("LEVEL1_TITLE");
        sql.SELECT("LEVEL2_TITLE");
        sql.SELECT("LEVEL3_TITLE");
        sql.SELECT("QUERY_TEXT");
        sql.SELECT("QUESTYLE");
        sql.SELECT("REQID");
        sql.SELECT("REQNAME");
        sql.SELECT("CMPDN");
        sql.SELECT("DEPTDN");
        sql.SELECT("CREATETIME");
        sql.SELECT("PRAISE");
        sql.SELECT("FINALANSWER");
        sql.SELECT("CLICK_SEARCH");
        sql.SELECT("ANSCODE");
        sql.SELECT("BANKID");
        sql.SELECT("SESSIONID");
        sql.SELECT("ANSCONTENT");
        sql.SELECT("VISIT_TYPE");
        sql.FROM("AITK_QA_RECORDS_V2");
        applyWhere(sql, example, false);

        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }

        return sql.toString();
    }

    public String selectByExample(QaRecordsV2Example example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("ID");
        } else {
            sql.SELECT("ID");
        }
        sql.SELECT("CATEGORY");
        sql.SELECT("QA_TYPE");
        sql.SELECT("LEVEL1_TITLE");
        sql.SELECT("LEVEL2_TITLE");
        sql.SELECT("LEVEL3_TITLE");
        sql.SELECT("QUERY_TEXT");
        sql.SELECT("QUESTYLE");
        sql.SELECT("REQID");
        sql.SELECT("REQNAME");
        sql.SELECT("CMPDN");
        sql.SELECT("DEPTDN");
        sql.SELECT("CREATETIME");
        sql.SELECT("PRAISE");
        sql.SELECT("FINALANSWER");
        sql.SELECT("CLICK_SEARCH");
        sql.SELECT("ANSCODE");
        sql.SELECT("BANKID");
        sql.SELECT("SESSIONID");
        sql.SELECT("VISIT_TYPE");
        sql.FROM("AITK_QA_RECORDS_V2");
        applyWhere(sql, example, false);

        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }

        return sql.toString();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        QaRecordsV2 record = (QaRecordsV2) parameter.get("record");
        QaRecordsV2Example example = (QaRecordsV2Example) parameter.get("example");

        SQL sql = new SQL();
        sql.UPDATE("AITK_QA_RECORDS_V2");

        if (record.getId() != null) {
            sql.SET("ID = #{record.id,jdbcType=VARCHAR}");
        }

        if (record.getCategory() != null) {
            sql.SET("CATEGORY = #{record.category,jdbcType=VARCHAR}");
        }

        if (record.getQaType() != null) {
            sql.SET("QA_TYPE = #{record.qaType,jdbcType=VARCHAR}");
        }

        if (record.getLevel1Title() != null) {
            sql.SET("LEVEL1_TITLE = #{record.level1Title,jdbcType=VARCHAR}");
        }

        if (record.getLevel2Title() != null) {
            sql.SET("LEVEL2_TITLE = #{record.level2Title,jdbcType=VARCHAR}");
        }

        if (record.getLevel3Title() != null) {
            sql.SET("LEVEL3_TITLE = #{record.level3Title,jdbcType=VARCHAR}");
        }

        if (record.getQueryText() != null) {
            sql.SET("QUERY_TEXT = #{record.queryText,jdbcType=VARCHAR}");
        }

        if (record.getQueStyle() != null) {
            sql.SET("QUESTYLE = #{record.queStyle,jdbcType=VARCHAR}");
        }

        if (record.getReqId() != null) {
            sql.SET("REQID = #{record.reqId,jdbcType=VARCHAR}");
        }

        if (record.getReqName() != null) {
            sql.SET("REQNAME = #{record.reqName,jdbcType=VARCHAR}");
        }

        if (record.getCmpDn() != null) {
            sql.SET("CMPDN = #{record.cmpDn,jdbcType=VARCHAR}");
        }

        if (record.getDeptDn() != null) {
            sql.SET("DEPTDN = #{record.deptDn,jdbcType=VARCHAR}");
        }

        if (record.getCreateTime() != null) {
            sql.SET("CREATETIME = #{record.createTime,jdbcType=TIMESTAMP}");
        }

        sql.SET("PRAISE = #{record.praise,jdbcType=DECIMAL}");

        sql.SET("FINALANSWER = #{record.finalAnswer,jdbcType=DECIMAL}");

        sql.SET("CLICK_SEARCH = #{record.clickSearch,jdbcType=DECIMAL}");

        sql.SET("ANSCODE = #{record.ansCode,jdbcType=DECIMAL}");

        if (record.getBankId() != null) {
            sql.SET("BANKID = #{record.bankId,jdbcType=VARCHAR}");
        }

        if (record.getSessionId() != null) {
            sql.SET("SESSIONID = #{record.sessionId,jdbcType=VARCHAR}");
        }

        if (record.getAnsContent() != null) {
            sql.SET("ANSCONTENT = #{record.ansContent,jdbcType=CLOB}");
        }

        if (record.getVisitType() != null) {
            sql.VALUES("VISIT_TYPE", "#{record.visitType,jdbcType=VARCHAR}");
        }

        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByExampleWithBLOBs(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("AITK_QA_RECORDS_V2");

        sql.SET("ID = #{record.id,jdbcType=VARCHAR}");
        sql.SET("CATEGORY = #{record.category,jdbcType=VARCHAR}");
        sql.SET("QA_TYPE = #{record.qaType,jdbcType=VARCHAR}");
        sql.SET("LEVEL1_TITLE = #{record.level1Title,jdbcType=VARCHAR}");
        sql.SET("LEVEL2_TITLE = #{record.level2Title,jdbcType=VARCHAR}");
        sql.SET("LEVEL3_TITLE = #{record.level3Title,jdbcType=VARCHAR}");
        sql.SET("QUERY_TEXT = #{record.queryText,jdbcType=VARCHAR}");
        sql.SET("QUESTYLE = #{record.queStyle,jdbcType=VARCHAR}");
        sql.SET("REQID = #{record.reqId,jdbcType=VARCHAR}");
        sql.SET("REQNAME = #{record.reqName,jdbcType=VARCHAR}");
        sql.SET("CMPDN = #{record.cmpDn,jdbcType=VARCHAR}");
        sql.SET("DEPTDN = #{record.deptDn,jdbcType=VARCHAR}");
        sql.SET("CREATETIME = #{record.createTime,jdbcType=TIMESTAMP}");
        sql.SET("PRAISE = #{record.praise,jdbcType=DECIMAL}");
        sql.SET("FINALANSWER = #{record.finalAnswer,jdbcType=DECIMAL}");
        sql.SET("CLICK_SEARCH = #{record.clickSearch,jdbcType=DECIMAL}");
        sql.SET("ANSCODE = #{record.ansCode,jdbcType=DECIMAL}");
        sql.SET("BANKID = #{record.bankId,jdbcType=VARCHAR}");
        sql.SET("SESSIONID = #{record.sessionId,jdbcType=VARCHAR}");
        sql.SET("ANSCONTENT = #{record.ansContent,jdbcType=CLOB}");
        sql.SET("VISIT_TYPE = #{record.visitType,jdbcType=VARCHAR}");

        QaRecordsV2Example example = (QaRecordsV2Example) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByExample(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("AITK_QA_RECORDS_V2");

        sql.SET("ID = #{record.id,jdbcType=VARCHAR}");
        sql.SET("CATEGORY = #{record.category,jdbcType=VARCHAR}");
        sql.SET("QA_TYPE = #{record.qaType,jdbcType=VARCHAR}");
        sql.SET("LEVEL1_TITLE = #{record.level1Title,jdbcType=VARCHAR}");
        sql.SET("LEVEL2_TITLE = #{record.level2Title,jdbcType=VARCHAR}");
        sql.SET("LEVEL3_TITLE = #{record.level3Title,jdbcType=VARCHAR}");
        sql.SET("QUERY_TEXT = #{record.queryText,jdbcType=VARCHAR}");
        sql.SET("QUESTYLE = #{record.queStyle,jdbcType=VARCHAR}");
        sql.SET("REQID = #{record.reqId,jdbcType=VARCHAR}");
        sql.SET("REQNAME = #{record.reqName,jdbcType=VARCHAR}");
        sql.SET("CMPDN = #{record.cmpDn,jdbcType=VARCHAR}");
        sql.SET("DEPTDN = #{record.deptDn,jdbcType=VARCHAR}");
        sql.SET("CREATETIME = #{record.createTime,jdbcType=TIMESTAMP}");
        sql.SET("PRAISE = #{record.praise,jdbcType=DECIMAL}");
        sql.SET("FINALANSWER = #{record.finalAnswer,jdbcType=DECIMAL}");
        sql.SET("CLICK_SEARCH = #{record.clickSearch,jdbcType=DECIMAL}");
        sql.SET("ANSCODE = #{record.ansCode,jdbcType=DECIMAL}");
        sql.SET("BANKID = #{record.bankId,jdbcType=VARCHAR}");
        sql.SET("SESSIONID = #{record.sessionId,jdbcType=VARCHAR}");
        sql.SET("VISIT_TYPE = #{record.visitType,jdbcType=VARCHAR}");

        QaRecordsV2Example example = (QaRecordsV2Example) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(QaRecordsV2 record) {
        SQL sql = new SQL();
        sql.UPDATE("AITK_QA_RECORDS_V2");

        if (record.getCategory() != null) {
            sql.SET("CATEGORY = #{category,jdbcType=VARCHAR}");
        }

        if (record.getQaType() != null) {
            sql.SET("QA_TYPE = #{qaType,jdbcType=VARCHAR}");
        }

        if (record.getLevel1Title() != null) {
            sql.SET("LEVEL1_TITLE = #{level1Title,jdbcType=VARCHAR}");
        }

        if (record.getLevel2Title() != null) {
            sql.SET("LEVEL2_TITLE = #{level2Title,jdbcType=VARCHAR}");
        }

        if (record.getLevel3Title() != null) {
            sql.SET("LEVEL3_TITLE = #{level3Title,jdbcType=VARCHAR}");
        }

        if (record.getQueryText() != null) {
            sql.SET("QUERY_TEXT = #{queryText,jdbcType=VARCHAR}");
        }

        if (record.getQueStyle() != null) {
            sql.SET("QUESTYLE = #{queStyle,jdbcType=VARCHAR}");
        }

        if (record.getReqId() != null) {
            sql.SET("REQID = #{reqId,jdbcType=VARCHAR}");
        }

        if (record.getReqName() != null) {
            sql.SET("REQNAME = #{reqName,jdbcType=VARCHAR}");
        }

        if (record.getCmpDn() != null) {
            sql.SET("CMPDN = #{cmpDn,jdbcType=VARCHAR}");
        }

        if (record.getDeptDn() != null) {
            sql.SET("DEPTDN = #{deptDn,jdbcType=VARCHAR}");
        }

        if (record.getCreateTime() != null) {
            sql.SET("CREATETIME = #{createTime,jdbcType=TIMESTAMP}");
        }

        sql.SET("PRAISE = #{praise,jdbcType=DECIMAL}");

        sql.SET("FINALANSWER = #{finalAnswer,jdbcType=DECIMAL}");

        sql.SET("CLICK_SEARCH = #{clickSearch,jdbcType=DECIMAL}");

        sql.SET("ANSCODE = #{ansCode,jdbcType=DECIMAL}");

        if (record.getBankId() != null) {
            sql.SET("BANKID = #{bankId,jdbcType=VARCHAR}");
        }

        if (record.getSessionId() != null) {
            sql.SET("SESSIONID = #{sessionId,jdbcType=VARCHAR}");
        }

        if (record.getAnsContent() != null) {
            sql.SET("ANSCONTENT = #{ansContent,jdbcType=CLOB}");
        }

        if (record.getVisitType() != null) {
            sql.SET("VISIT_TYPE = #{visitType,jdbcType=VARCHAR}");
        }

        sql.WHERE("ID = #{id,jdbcType=VARCHAR}");

        return sql.toString();
    }

    protected void applyWhere(SQL sql, QaRecordsV2Example example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }

        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }

        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }

                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }

                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }

        if (sb.length() > 0) {
            sql.WHERE(sb.toString());
        }
    }
}
